import { compare, isInArray } from "@/utils";

export default class PowerCreepMissonWork extends PowerCreep {

    public ManageMisson():void{
        /* 获取名字 */
        var name = this.name
        var info = name.split('/')
        /* pc姓名 如： E41S45/home/shard3/1 */
        if (info.length != 4) {this.say("名字有问题!");return}
        if (!this.memory.belong) this.memory.belong =info[0]    // 所属房间
        if (!this.memory.role) this.memory.role = info[1]   // 角色
        if (!this.memory.shard) this.memory.shard = info[2] as shardName
        if (!Game.rooms[this.memory.belong]) return
        var thisSpawn = global.Stru[this.memory.belong]['powerspawn'] as StructurePowerSpawn
        if (!thisSpawn) return
        if (!this.memory.spawn)
        {
            this.memory.spawn = thisSpawn.id
            
        }
        if(!Game.rooms[this.memory.belong].controller.isPowerEnabled)
        {
            /* 没有允许Power就自动激活power开关 */
            if (!this.pos.isNearTo(Game.rooms[this.memory.belong].controller)) this.goTo(Game.rooms[this.memory.belong].controller.pos,1)
            else this.enableRoom(Game.rooms[this.memory.belong].controller)
            return
        }
        if (this.ticksToLive < 1000 && this.memory.shard == Game.shard.name)
        {
            if (!this.pos.isNearTo(thisSpawn)) {this.goTo(thisSpawn.pos,1)}
            else this.renew(thisSpawn)
            return
        }
        var storage_ = global.Stru[this.memory.belong]['storage'] as StructureStorage
        if (!storage_) return
        if (this.store.getUsedCapacity('ops') < Math.ceil(this.store.getCapacity()/4))
        {
            if(!this.powers[PWR_GENERATE_OPS].cooldown) {this.usePower(PWR_GENERATE_OPS)}
            if (!storage_ || storage_.store.getUsedCapacity('ops') < Math.ceil(this.store.getCapacity()/2)) return
            else
            {
                if (!this.pos.isNearTo(storage_)) this.goTo(storage_.pos,1)
                else this.withdraw(storage_,'ops',Math.ceil(this.store.getCapacity()/2))
            }
            this.say("🙅‍")
            return
        }
        if (!this.memory.MissonData) this.memory.MissonData = {}
        if (!Game.rooms[this.memory.belong].memory.Misson['PowerCreep'])
                Game.rooms[this.memory.belong].memory.Misson['PowerCreep'] = []
        if (Object.keys(this.memory.MissonData).length <= 0){
            /* 领取任务 */
                var taskList = Game.rooms[this.memory.belong].memory.Misson['PowerCreep']
                var thisTaskList:MissionModel[] = []
                for (var Stask of taskList)
                {
                if (Stask.CreepBind && isInArray(Object.keys(Stask.CreepBind),this.memory.role))
                    thisTaskList.push(Stask)
                }
                    /* 根据优先等级排列，领取最优先的任务 */
                    thisTaskList.sort(compare('level'))
                    /* 还没有绑定的任务，就等待接取任务 */
                    LoopBind:
                    for (var t of thisTaskList)
                    {
                        if (t.CreepBind && t.CreepBind[this.memory.role] && t.CreepBind[this.memory.role].bind.length < t.CreepBind[this.memory.role].num)
                        {
                            /* 绑定任务了就输入任务数据 */
                            t.processing = true // 领取任务后，任务开始计时
                            t.CreepBind[this.memory.role].bind.push(this.name)
                            this.memory.MissonData.MisssonID = t.id           // 任务id
                            this.memory.MissonData.MissonName = t.name        // 任务名
                            this.memory.MissonData.MissonDelay = 150  // 爬虫处理任务的超时时间
                            this.memory.MissonData.data = t.data?t.data:{}    // 任务数据传输
                            break LoopBind
                        }
                    }
                    if (Object.keys(this.memory.MissonData).length <= 0)
                    {
                        /* 没有任务就生产ops */
                        if(!this.powers[PWR_GENERATE_OPS].cooldown) {this.usePower(PWR_GENERATE_OPS)}
                    }
                    return
        }
        else
        {
            /* 处理任务 */
            this.memory.MissonData.MissonDelay --     // 爬虫内置Tick计时
            if (this.memory.MissonData.MissonDelay <= 0)
            {
                this.memory.MissonData = {}
                return
            }
            switch (this.memory.MissonData.MissonName) 
            {
            case "仓库扩容":{this.handle_pwr_storage(); break;}
            case '强化防御塔':{this.handle_pwr_tower();break;}
            case 'lab加速':{this.handle_pwr_lab();break;}
            case '操作扩展':{this.handle_pwr_extension();break;}
            case '虫卵强化':{this.handle_pwr_spawn();break;}
            }
        }
    }
}